package com.goods.business.service.imp;

import com.goods.business.mapper.*;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.OutStockItemVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.business.OutStockVolist;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class OutStockServiceImpl implements OutStockService {

    @Autowired
    private OutStockMapper outStockMapper;

    @Override
    public List<OutStockVO> findOutStockList(Integer status, Integer type, String outNum) {
        Map map = new HashMap<>();
        map.put("status", status);
        map.put("type", type);
        map.put("outNum", outNum);
        List<OutStockVO> outStockList = outStockMapper.findOutStockList(map);
        return outStockList;
    }

    @Override
    public void addOutStock(OutStockVO outStockVO) {
        String s = UUID.randomUUID().toString();
        String out_num = StringUtils.replace(s, "-", "");
        //去除地址表
        Long consumerId = 0L;
        if (outStockVO.getConsumerId() == null) {
            Consumer consumer = new Consumer();
            consumer.setId(null);
            consumer.setName(outStockVO.getName());
            consumer.setAddress(outStockVO.getAddress());
            consumer.setContact(outStockVO.getContact());
            consumer.setPhone(outStockVO.getPhone());
            consumer.setSort(outStockVO.getSort());
            consumer.setCreateTime(Calendar.getInstance().getTime());
            consumer.setModifiedTime(Calendar.getInstance().getTime());
            consumerMapper.insert(consumer);
            consumerId = consumer.getId();
        }
        //添加去处表
        OutStock outStock = new OutStock();
        BeanUtils.copyProperties(outStockVO, outStock);
        outStock.setId(null);
        outStock.setOutNum(out_num);
        outStock.setType(outStockVO.getType());
        outStock.setOperator("admin");
        outStock.setCreateTime(Calendar.getInstance().getTime());
        if (outStockVO.getConsumerId() == null) {
            outStock.setConsumerId(consumerId);
        }
        outStock.setConsumerId(outStock.getConsumerId());
        outStock.setStatus(2);
        outStock.setProductNumber(outStockVO.getProducts().size());
        outStockMapper.insert(outStock);
        //去处详情表
        for (OutStockVolist product : outStockVO.getProducts()) {
            Product product1 = productMapper.selectByPrimaryKey(product.getProductId());
            OutStockInfo outStockInfo = new OutStockInfo();
            outStockInfo.setId(null);
            outStockInfo.setOutNum(out_num);
            outStockInfo.setPNum(product1.getPNum());
            outStockInfo.setProductNumber(product.getProductNumber());
            outStockInfo.setCreateTime(Calendar.getInstance().getTime());
            outStockInfo.setModifiedTime(Calendar.getInstance().getTime());
            outStockInfoMapper.insert(outStockInfo);
           /* //更新库存表
            ProductStock productStock = new ProductStock();
            productStock.setPNum(product1.getPNum());
            ProductStock productStocks = productStockMapper.selectOne(productStock);
            productStocks.setStock(productStocks.getStock()-product.getProductNumber());
            productStockMapper.updateByPrimaryKeySelective(productStocks);*/
        }
    }

    @Override
    public void delete(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStockMapper.deleteByPrimaryKey(id);
        OutStockInfo outStockInfo = new OutStockInfo();
        outStockInfo.setOutNum(outStock.getOutNum());
        outStockInfoMapper.delete(outStockInfo);
    }

    /**
     * 返回值
     * this.detailTable = res.data.itemVOS
     * this.detailTotal = res.data.total
     * this.consumer = res.data.consumerVO
     * this.pStatus = res.data.status
     */
    @Override
    public Map detail(Long id) {
        List<OutStockItemVO> list = outStockMapper.detail(id);
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectOne(outStock);
        Consumer consumer = consumerMapper.selectByPrimaryKey(outStock1.getConsumerId());
        Map map = new HashMap<>();
        map.put("itemVOS", list);
        map.put("total", list.size());
        map.put("consumerVO", consumer);
        OutStock outStock2 = outStockMapper.selectByPrimaryKey(id);
        map.put("status", outStock2.getStatus());
        return map;
    }

    @Override
    public void publish(Long id) {
        //更新库存，
        // 先查出每个商品的库存，
        //库存--
        //更改状态
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKeySelective(outStock);
        OutStock outStock1 = outStockMapper.selectByPrimaryKey(id);

        OutStockInfo outStockInfo = new OutStockInfo();
        outStockInfo.setOutNum(outStock1.getOutNum());
        List<OutStockInfo> select = outStockInfoMapper.select(outStockInfo);
        for (OutStockInfo stockInfo : select) {
            ProductStock productStock = new ProductStock();
            productStock.setPNum(stockInfo.getPNum());
            ProductStock productStock1 = productStockMapper.selectOne(productStock);
            productStock1.setStock(productStock1.getStock() - stockInfo.getProductNumber());
            productStockMapper.updateByPrimaryKeySelective(productStock1);
        }

    }

    @Autowired
    private ConsumerMapper consumerMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OutStockInfoMapper outStockInfoMapper;
    @Autowired
    private ProductStockMapper productStockMapper;
}
